<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Appstart extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('appstart')->count();// 获取总条数
            $lists = Db::name('appstart')
                ->field('banner_id, banner_title, banner_img, banner_status, 
                 banner_sort, banner_time, banner_update_time')
                ->page($Nowpage,$limits)
                ->order('banner_status asc,banner_sort asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_img = input('banner_img');
            $banner_status = input('banner_status');
            $banner_sort = input('banner_sort');
            $data = [
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_sort' => $banner_sort,
                'banner_status' => $banner_status,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];

            $res = Db::table('up_appstart')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }

        return $this->fetch('appstart/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('banner_id');
            $banner_title = trim(input('banner_title'), ' ');
            $banner_img = input('banner_img');
            $data = [
                'banner_title' => $banner_title,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            
            if(!empty($banner_img)){
                $data['banner_img'] = $banner_img;
            }
            $res = Db::table('up_appstart')->where('banner_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_appstart')
            ->where('banner_id', $banner_id)
            ->find();

        $this->assign([
            'banner_id' => $banner_id,
            'banner' => $banner,
        ]);
        return $this->fetch('appstart/banneredit');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('appstart')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_appstart')->where('banner_id', $banner_id)->setField('banner_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 200, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_appstart')->where('banner_id', $banner_id)->setField('banner_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 200, 'msg' => '下架失败']);
        }
    }
}